﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MM.Common.DBBase;
using MM.Common.Base;
using System.Data;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using MM.Common.UI.Controls;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.Utils;
using DevExpress.XtraRichEdit;
using MM.Commmon.UI;

namespace MM.Common.Information
{
    public partial class FrmNoticeDisplay : FrmFunctionBase
    {
        public DataTable dtBrowser = new DataTable();
        public FrmNoticeDisplay()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            this.SetRead();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.RefreshNotice();
        }

        protected void FreshRtf(int row)
        {
            this.rtbContent.RtfText = "";
            if (row >= 0)
            {
                this.rtbContent.RtfText = this.dtBrowser.Rows[this.gvNotice.GetDataSourceRowIndex(row)]["NOTICE_CONTENT"].ToString();
            }
        }

        private void FrmDisplayNotice_Load(object sender, EventArgs e)
        {
            this.RefreshNotice();
        }

        private void gvNotice_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            this.FreshRtf(e.FocusedRowHandle);
        }
        public void RefreshNotice()
        {
            string commandtext = "";
            if (base.FunctionParameter != "Popup")
            {
                commandtext = "SELECT DTL.NOTICE_NO, INFO.NOTICE_TITLE, INFO.NOTICE_CONTENT ,DTL.STATUS \r\n   FROM KTSYS_NOTICE_DETAIL DTL ,KTSYS_NOTICE_INFO INFO \r\n   WHERE DTL.NOTICE_NO = INFO.NOTICE_NO AND DTL.EMP_ID = :EMP_ID AND (DTL.STATUS = 'R' OR DTL.STATUS = 'N') \r\n   ORDER BY DTL.NOTICE_NO DESC";
            }
            else
            {
                commandtext = "SELECT DTL.NOTICE_NO, INFO.NOTICE_TITLE, INFO.NOTICE_CONTENT ,DTL.STATUS \r\n   FROM KTSYS_NOTICE_DETAIL DTL ,KTSYS_NOTICE_INFO INFO \r\n   WHERE DTL.NOTICE_NO = INFO.NOTICE_NO AND DTL.EMP_ID = :EMP_ID AND DTL.STATUS = 'N' \r\n   ORDER BY DTL.NOTICE_NO DESC";
            }
            DataParamCol parm = new DataParamCol();
            parm.Add("emp_id", SystemInfo.OperId);
            this.dtBrowser.Clear();
            SystemInfo.DbData.Retrieve(commandtext, parm, ref this.dtBrowser);
            this.gvNotice.GridControl.DataSource = this.dtBrowser;
        }

        protected void SetRead()
        {
            if (this.gvNotice.FocusedRowHandle >= 0)
            {
                if (this.gvNotice.GetFocusedRowCellValue("STATUS").ToString() == "R")
                {
                    if (this.gvNotice.RowCount == 1)
                    {
                        base.Close();
                    }
                    else
                    {
                        this.gvNotice.DeleteRow(this.gvNotice.FocusedRowHandle);
                        this.rtbContent.RtfText = this.gvNotice.GetFocusedRowCellValue("NOTICE_CONTENT").ToString();
                    }
                }
                else
                {
                    int paravalue = Convert.ToInt32(this.gvNotice.GetFocusedRowCellValue("NOTICE_NO"));
                    string commandtext = "UPDATE KTSYS_NOTICE_DETAIL SET STATUS = 'R' WHERE EMP_ID = :emp_id AND NOTICE_NO = :notice_no";
                    DataParamCol parms = new DataParamCol();
                    parms.Add("emp_id", SystemInfo.OperId);
                    parms.Add("notice_no", paravalue);
                    if (SystemInfo.DbData.ExecuteSql(commandtext, parms) > 0)
                    {
                        if (this.gvNotice.RowCount == 1)
                        {
                            base.Close();
                        }
                        else
                        {
                            this.gvNotice.DeleteRow(this.gvNotice.FocusedRowHandle);
                            this.rtbContent.RtfText = this.gvNotice.GetFocusedRowCellValue("NOTICE_CONTENT").ToString();
                        }
                    }
                }
            }
        }
    }
}
